package com.turingoal.common.util.spring;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
import com.turingoal.common.constants.ConstantDateFormatTypes;

/**
 * spring mvc form 类型转换
 */
public class SpringWebBinding implements WebBindingInitializer {
	/**
	 * form 类型转换
	 */
	public final void initBinder(final WebDataBinder binder, final WebRequest request) {
		// 1. 使用spring自带的CustomDateEditor
		SimpleDateFormat dateFormat = new SimpleDateFormat(ConstantDateFormatTypes.YYYY_MM_DD);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
		// 2. 自定义的PropertyEditorSupport
		// binder.registerCustomEditor(Date.class, new DateConvertEditor());
		// 字符串trim
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
		// 对具体字段进行转换
	}
}
